//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    maskFlag: true,
    timeFlag: true,
    areaFlag: true,
    timeArr: [],
    areaArr: [],
    time: "",
    timeId: "",
    area: "",
    areaId: "",
    uploadImg: "../../images/uploadImg.png",
    orderNumber: "",
    fee: "",
    price: "",
    // 
    attractDiscount: "",
    attractInvestmentFreeDay:"",
    freeAttractInvestmentNum:"",
    // 
    uid:"",
    userType:"",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })
    this.CheckAttractInvestmentIsFirst();
    this.GetAttractInvestmentTime();
    this.GetAttractInvestmentArea();
  },

  
  // CheckAttractInvestmentIsFirst，获取招商免费次数
  CheckAttractInvestmentIsFirst: function() {
    var that = this;
    var uid = this.data.uid;
    var attractDiscount = this.data.attractDiscount;
    var freeAttractInvestmentNum = this.data.freeAttractInvestmentNum;
    var v = this.data.attractInvestmentFreeDay;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckAttractInvestmentIsFirst&UID=" + uid),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(json);
        that.setData({
          attractDiscount: parseFloat(json.AttractDiscount).toFixed(2),
          freeAttractInvestmentNum: json.FreeAttractInvestmentNum,
          attractInvestmentFreeDay: json.AttractInvestmentFreeDay,

        })
      }
    })

  },
  // GetAttractInvestmentTime，获取招商时长
  GetAttractInvestmentTime: function() {
    var that = this;
    var uid = this.data.uid;
    var timeArr = this.data.timeArr;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetAttractInvestmentTime"),
      success: function(res) {
        timeArr = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        that.setData({
          timeArr: timeArr
        })
        // console.log(timeArr);
      }
    })

  },
  // GetAttractInvestmentTime，获取招商区域
  GetAttractInvestmentArea: function() {
    var that = this;
    var uid = this.data.uid;
    var areaArr = this.data.areaArr;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetAttractInvestmentArea"),
      success: function(res) {
        areaArr = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        // console.log(areaArr);
        that.setData({
          areaArr: areaArr,
        })

      }
    })
  },
  // maskTap
  maskTap: function() {
    this.setData({
      maskFlag: !this.data.maskFlag,
      timeFlag: !this.data.timeFlag,
    })
  },
  // timeModalShow
  timeModalShow: function() {
    this.setData({
      maskFlag: !this.data.maskFlag,
      timeFlag: !this.data.timeFlag,
    })
  },
  // chooseTime
  chooseTime: function(e) {
    // console.log(e); 
    this.setData({
      time: e.currentTarget.dataset.time,
      timeId: e.currentTarget.dataset.timeid,
      maskFlag: !this.data.maskFlag,
      timeFlag: !this.data.timeFlag,
      fee: e.currentTarget.dataset.time.split("个")[0] * this.data.price * (this.data.attractDiscount / 100),
    })
  },
  // areaModalShow
  areaModalShow: function() {
    this.setData({
      maskFlag: !this.data.maskFlag,
      areaFlag: !this.data.areaFlag,
    })
  },
  // chooseArea
  chooseArea: function(e) {
    var that = this;
    var time = this.data.time;

    // console.log(e);
    this.setData({
      area: e.currentTarget.dataset.area,
      areaId: e.currentTarget.dataset.areaid,
      maskFlag: !this.data.maskFlag,
      areaFlag: !this.data.areaFlag,
      price: parseFloat(e.currentTarget.dataset.price).toFixed(2),
      fee: time.split("个")[0] * parseFloat(e.currentTarget.dataset.price).toFixed(2) * (this.data.attractDiscount / 100),
    })
  },
  // chooseImg
  chooseImg: function(e) {
    var that = this;
    var base64 = this.data.base64;
    var imgSrc = this.data.imgSrc;
    var imgUrl = this.data.imgUrl;
    var uploadImg = this.data.uploadImg;
    wx.chooseImage({
      // 最多可以选择的图片张数，默认9
      count: 1,
      // 可以指定是原图还是压缩图，默认二者都有
      sizeType: ['original', 'compressed'],
      // 可以指定来源是相册还是相机，默认二者都有
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        wx.request({
          url: tempFilePaths,
          method: 'GET',
          responseType: 'arraybuffer',
          success: function(res) {
            base64 = wx.arrayBufferToBase64(res.data);
            that.setData({
              base64: base64,
              imgSrc: 'data:image/jpg;base64,' + base64
            })
            // console.log(base64)
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=LoadPic"),
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: 'post',
              data: baseJs.json2Form({
                HeadImage: base64,
              }),
              success: function(res) {
                console.log(res);
                that.setData({
                  uploadImg: imgUrl + res.data.ResultData,
                })
              }
            })
          }
        })
      }
    })
  },
  formSubmit: function(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var that = this;
    var uid = this.data.uid;
    var timeId = this.data.timeId;
    var areaId = this.data.areaId;
    var uploadImg = this.data.uploadImg;
    var orderNumber = this.data.orderNumber;
    var price = this.data.price;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=PublishAttractInvestment&UID=" + uid + "&Title=" + e.detail.value.title + "&Times=" + timeId + "&Areas=" + areaId + "&Images=" + uploadImg),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.navigateTo({
            url: '/packageA/findBusinessPay/findBusinessPay?orderNumber=' + json.ResultCount + '&price=' + price,
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })

  },
  // 
  formReset: function() {
    wx.navigateBack({
      delta: 1
    })
  },
})